const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0';
const app = getApp();
const s = new app.Shuju();
Page({
  data: {
    avatarUrl: defaultAvatarUrl,
  },
  onChooseAvatar(e) {
    const {
      avatarUrl
    } = e.detail;
    this.setData({
      avatarUrl
    })
  },
  _UpPic: function (file) {
    console.log(file)
    return new Promise((succ, err) => {
      wx.uploadFile({
        url: app.upfilephp,
        filePath: file,
        name: 'file',
        formData: {
          'method': 'upfile'
        },
        success(res) {
          const data = JSON.parse(res.data);
          succ(app.baseimg + data.data);
        },
        complete: function () {
          wx.hideLoading({
            success: (res) => {},
          })
        }
      })
    })
  },
  _Queding: function (e) {
    console.log(e);
    let wxname = e.detail.value.wxname;
    let wxhead = this.data.avatarUrl;
    if (wxname == '' || wxhead == defaultAvatarUrl) {
      app.te('请上传头像和昵称');
    } else {
      wx.setStorageSync('wxname', wxname);
      wx.setStorageSync('wxhead', wxhead);
      this._UpPic(wxhead).then((d) => {
        s.se('wxhead', d);
        s.se('wxname', wxname);
        s.se('wxid', wx.getStorageSync('wxid'));
        s.g('c/user_wxlogin', {
          s: function (data) {
            if (data.code == 200) {
              app.ts('登录成功', function () {
                wx.setStorageSync('id', data.data.id);
                wx.navigateBack({
                  delta: 2,
                })
              })
            } else {
              app.te('登录失败');
            }
          }
        })
      })
    }
  }
})